package com.young.ysepay.service.impl;

import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.Producer;
import com.young.ysepay.mq.YsReturnMQConfig;
import com.young.ysepay.service.YsAsyncWithdrawalService;
import com.young.ysepay.service.YsNotifyRegistryService;
import com.young.ysepay.service.YsVerifyJsonSignService;
import com.young.ysepay.utils.JSONUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Map;

/**
 * describe:
 *
 * @author zhaopengjun
 * @date 2019/6/10 18:02
 */
@Service
public class YsAsyncWithdrawalServiceImpl implements YsAsyncWithdrawalService {
    @Autowired
    YsNotifyRegistryService ysNotifyRegistryService;
    @Autowired
    YsVerifyJsonSignService ysVerifyJsonSignService;
    @Autowired(required = false)
    private Producer producer;
    private static Logger log = LoggerFactory.getLogger(YsAsyncWithdrawalServiceImpl.class);
    @Override
    public void asyncWithdrawal(Map<String,String> paramMap, HttpServletResponse response) {
        PrintWriter writer = null;
        try {
            Message message = null;
            writer = response.getWriter();
            boolean verifyResult = ysVerifyJsonSignService.verifySign(paramMap);
            // 状态
            if (verifyResult) {// 验证成功
                message = new Message(YsReturnMQConfig.SETTLETOPIC, YsReturnMQConfig.YSWITHDRAWALRECEIPT_TAG, JSONUtils.objectToJson(paramMap).getBytes());
                producer.send(message);
                log.error("银盛实时提现异步通知发送消息"+new String(message.getBody()));
                writer.print("success");

            } else {// 验证失败
                log.error("银盛实时提现异步通知错误消息"+new String(message.getBody()));
                writer.print("fail");
            }
        } catch (Exception e) {
            log.error("银盛实时提现异步接口调用失败@！",e.getMessage());
            e.printStackTrace();
        }finally {
            if (writer != null){
                writer.close();
            }
        }

    }
}
